import { toStringTypeCheck } from './toStringTypeCheck';

/**
 * 挂载 唯一元素 并绑定 值对象
 * @param {Object | Array} objSource 目标挂载元素
 * @param {String | any} key 新挂载名称
 * @param {any} obj 新 挂载 名称 的 值 对象
 * @param {String} data 挂载预定位置 默认为 "data" 需要 该预设位置为 数组
 * @returns {boolean}
 */
function ObjectOnlyPushSet(objSource, key, obj, data = 'data') {
  if (toStringTypeCheck(objSource, 'array')) {
    if (objSource.indexOf(key) == -1) {
      objSource.push(key);
      return true;
    }
    // console.log(`sorry your key:${key} is aready in your Array`);
  }
  if (objSource[data] == undefined) {
    //   console.log(`sorry your ${data} is not a available prop`);
    return false;
  }
  if (toStringTypeCheck(objSource[data], 'array')) {
    if (!objSource[data].includes(key)) {
      objSource[data].push(key);
      if (obj != undefined) {
        objSource[key] = obj;
      }
      return true;
    }
  }
  // console.log(`sorry your key:${key} is aready in your Object[${data}]`);
  return false;
}

/**
 * 卸载 唯一元素 并解除绑定 值对象
 * -
 * @param {Object | Array} objSource 目标挂载元素
 * @param {String} key 卸载名称
 * @param {boolean} [deleteval = true] 卸载 挂载 的 属性
 * @param {String} data 卸载预定位置 默认为 "data" 需要 该预设位置为 数组
 * @returns
 */
function removeObjectOnlyPushSet(
  objSource,
  key,
  deleteval = true,
  data = 'data'
) {
  let ind;
  if (toStringTypeCheck(objSource, 'array')) {
    ind = objSource.indexOf(key);
    if (ind >= -1) {
      objSource.splice(ind, 1);
      return true;
    }
    return false;
    // console.log(`sorry your key:${key} is not in your Array`);
  }
  if (objSource[data] == undefined) {
    //   console.log(`sorry your ${data} is not a available prop`);
    return false;
  }

  if (toStringTypeCheck(objSource[data], 'array')) {
    ind = objSource[data].indexOf(key);
    if (ind > -1) {
      objSource[data].splice(ind, 1);
      if (deleteval == true) {
        delete objSource[key];
      }
      return true;
    }
  }
  // console.log(`sorry your key:${key} is aready in your Object[${data}]`);
  return false;
}

export { ObjectOnlyPushSet, removeObjectOnlyPushSet };
